<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/5/12
 * Time: 2:10 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Payments_PaymentMethodsFields extends Page{
	/**
	 * @var Db_Payments_PaymentMethodsFields
	 */
	protected $tableObject;

	function __construct() {
		$this->idTableObject = get('id', 'int');
		$this->classTableObject = 'Db_Payments_PaymentMethodsFields';
		$this->actionsTableObject = array(
			'addEditAction',
			'saveAction',
			'ajaxLoadDataAction'
		);
		parent::__construct();
	}

	public function defaultAction() {
		self::$TPL->appendJsFile('admin/payment_methods_fields');
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
		self::$TPL->setBodyTemplate('list_payment_methods_fields');
		return array(
			'type' => 'display'
		);
	}

	public function ajaxLoadDataAction() {

		$allColumns = array('nr', 'pmf_field_name', 'pmf_field_type', 'pmf_field_type', 'pm_name');
		$searchColumns = array('pmf_field_name', 'pmf_field_type', 'pmf_field_type', 'pm_name');

		list($searchQuery,$orderBy,$limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, "Db_Payments_PaymentMethodsFields", "getTotalPaymentMethodsFields");

		$allPaymentMethodsFields = $this->tableObject->getAllPaymentMethodsFields($limit, $searchQuery, $orderBy);
		self::$TPL->assign('allPaymentMethodsFields', $allPaymentMethodsFields);

		return array(
			'type' => 'encoded_json',
			'content' => self::$TPL->display('json_list_payment_methods_fields', true)
		);
	}

	public function addEditAction() {
		perm('PAYMENT_METHODS_FIELDS_EDIT', self::$PAGE['p_url']);
		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') .' "'. $this->tableObject->objectArray->pmf_field_name . '"' , '?action=addEdit', true);
			self::$TPL->assign('paymentMethodsFields', $this->tableObject->objectArray);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
		}

		$types = Db_Payments_PaymentMethodsFields::getEnumValues('pmf_field_type');
		self::$TPL->assign('types', $types);
		$paymentMethods = Db_Payments_PaymentMethods::findAll();
		self::$DEBUG->addPrivateVar($paymentMethods);
		self::$TPL->assign('paymentMethods', $paymentMethods);

		self::$TPL->setBodyTemplate('add_edit_payment_method_fields');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction() {
		perm('PAYMENT_METHODS_FIELDS_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		$this->tableObject->pmf_field_name = post('pmf_field_name', 'str');
		$this->tableObject->pmf_field_type = post('pmf_field_type', 'str');
		$this->tableObject->pmf_payment_method_id = post('pmf_payment_method_id', 'int');

		if(!empty($this->tableObject->pmf_field_name) && !empty($this->tableObject->pmf_field_type) && !empty($this->tableObject->pmf_payment_method_id)) {
			if(!$this->tableObject->save()) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		}

		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function deleteAction() {
		perm('PAYMENT_METHODS_FIELDS_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		if(!Db_Payments_PaymentMethodsFields::abstractDelete($this->idTableObject)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}
}
